﻿namespace XUCore.Win32.Test
{
    /// <summary>
    /// 扣缴端登录
    /// </summary>
    public static class Withhold
    {
        private static PROCESS_INFORMATION processInfo;
        /// <summary>
        /// 启动程序
        /// </summary>
        /// <exception cref="Exception"></exception>
        public static void StartProcess()
        {
            string argm = @"C:\ITSKHD\EPPortal_DS3.0\EPEvenue_SH.exe";

            STARTUPINFO sInfo = new STARTUPINFO();

            if (!Kernel32.CreateProcess(argm, "", IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref sInfo, ref processInfo))
            {
                throw new Exception("启动失败");
            }

            Kernel32.WaitForSingleObject(processInfo.hProcess, int.MaxValue);
        }
        /// <summary>
        /// 关闭程序
        /// </summary>
        public static void CloseProcess()
        {
            uint i = 0;
            Kernel32.TerminateProcess(processInfo.hProcess, 1);
            Kernel32.GetExitCodeProcess(processInfo.hProcess, ref i);
            Kernel32.CloseHandle(processInfo.hProcess);
            Kernel32.CloseHandle(processInfo.hThread);
        }
        /// <summary>
        /// 获取登录Viewer
        /// </summary>
        /// <returns></returns>
        public static IntPtr GetLoginViewer()
        {
            var loginViewer = Windows.FindMainWindow("Tfrm_LoginViewer", "frm_LoginViewer");

            var ss1 = loginViewer.FindChild(IntPtr.Zero, "TJdlsPanel", "");

            var ss2_1 = ss1.FindChild(IntPtr.Zero, "TJdlsPanel", "");
            var ss2_2 = ss1.FindChild(ss2_1, "TJdlsPanel", "");
            var ss2_3 = ss1.FindChild(ss2_2, "TJdlsPanel", "");
            var ss3 = ss2_3.FindChild(IntPtr.Zero, "TXPFarmerPageControlSP", "");

            var spITSOnline = ss3.FindChild(IntPtr.Zero, "TXPFarmerTabSheetSP", "spITSOnline");

            return spITSOnline;
        }
        /// <summary>
        /// 切换登录方式（1、实名登录，2、申报密码登录）
        /// </summary>
        /// <param name="hwndParent"></param>
        public static void SwitchLogin(IntPtr hwndParent, int cutMode)
        {
            var loginPanel = hwndParent.FindChild(IntPtr.Zero, "TJdlsPanel", "");
            switch (cutMode)
            {
                case 1:
                    {
                        var tabPhone = loginPanel.FindChild(IntPtr.Zero, "TITSTabControl", "");

                        tabPhone.Click();
                    }
                    break;
                case 2:
                    {
                        var tabPhone = loginPanel.FindChild(IntPtr.Zero, "TITSTabControl", "");
                        var tabPassword = loginPanel.FindChild(tabPhone, "TITSTabControl", "");

                        tabPassword.Click();
                    }
                    break;
            }
        }
        /// <summary>
        /// 账号密码登录
        /// </summary>
        /// <param name="hwndParent"></param>
        /// <param name="phone"></param>
        /// <param name="password"></param>
        public static void PasswordLogin(IntPtr hwndParent, string phone, string password)
        {
            var txPFarmerPageControlSP = hwndParent.FindChild(IntPtr.Zero, "TXPFarmerPageControlSP", "");
            var spAccountPanel = txPFarmerPageControlSP.FindChild(IntPtr.Zero, "TXPFarmerTabSheetSP", "spAccount");

            var verifyCodeHandler = spAccountPanel.FindChild(IntPtr.Zero, "TXPFamerEditEx", "");
            var loginButtonHandler = spAccountPanel.FindChild(IntPtr.Zero, "TITSSpeedButton", "");
            var passwordHandler = spAccountPanel.FindChild(verifyCodeHandler, "TXPFamerEditEx", "");
            var phoneHandler = spAccountPanel.FindChild(passwordHandler, "TXPFamerEditEx", "");

            phoneHandler.SetText(phone);
            passwordHandler.SetText(password);

            loginButtonHandler.Click();
        }
        /// <summary>
        /// 申报密码登录
        /// </summary>
        /// <param name="hwndParent"></param>
        /// <param name="companyName"></param>
        /// <param name="password"></param>
        public static void DeclarePasswordLogin(IntPtr hwndParent, string companyName, string password)
        {
            var txPFarmerPageControlSP = hwndParent.FindChild(IntPtr.Zero, "TXPFarmerPageControlSP", "");
            var spAccountPanel = txPFarmerPageControlSP.FindChild(IntPtr.Zero, "TXPFarmerTabSheetSP", "spDeclare");

            //点击搜索框
            var txt_CompanySearch = spAccountPanel.FindChild(IntPtr.Zero, "TIntelligentSearch", "");

            txt_CompanySearch.Click();

            //弹出下拉菜单
            var listViewer = Windows.FindMainWindow("Tfrm_IntelligentSearchPop", "");
            var companyNameHandler = listViewer.FindChild(IntPtr.Zero, "TXPFamerEditW.W", "");
            var listHandler = listViewer.FindChild(IntPtr.Zero, "TListBox.W", "");
            //写入预填的公司
            companyNameHandler.SetText(companyName);
            //定位下拉框ComboBoxlist
            var listCount = listHandler.GetComboBoxCount();

            for (var i = 0; i < listCount; i++)
            {
                //获取当前下拉框的数据
                var name = listHandler.GetComboBoxSelectItemText(i);

                if (name == companyName)
                {
                    listHandler.SetComboBoxSelectItem(i);

                    continue;
                }
            }

            //输入密码或者验证码（此处一般不会出问题，所以不会出现验证码）
            var verifyCodeHandler = spAccountPanel.FindChild(IntPtr.Zero, "TXPFamerEditEx", "");
            var passwordHandler = spAccountPanel.FindChild(verifyCodeHandler, "TXPFamerEditEx", "");
            var loginButtonHandler = spAccountPanel.FindChild(IntPtr.Zero, "TITSSpeedButton", "");

            passwordHandler.SetText(password);

            ////查找验证码弹出警告框
            //var msgDlgRich = User32.FindWindow("Tfrm_MsgDlgRich", "");
            //if(msgDlgRich != IntPtr.Zero)
            //{
            //    var pnl_body = FindElement(msgDlgRich, IntPtr.Zero, "TPanel", "pnl_body");
            //    var pnl = FindElement(pnl_body, IntPtr.Zero, "TPanel", "");
            //    var btn_pnl = FindElement(pnl, IntPtr.Zero, "TITSSpeedButton", "");

            //    User32.SendMessage(btn_pnl, Const.WM_LBUTTONDOWN, 0, 0);
            //    Thread.Sleep(100);
            //    User32.SendMessage(btn_pnl, Const.WM_LBUTTONUP, 0, 0);
            //}

            //User32.SendMessage(txt_VerifyCode, Const.WM_SETTEXT, 0, new StringBuilder(verifyCode));

            var bitmap = Windows.PrintWindow(verifyCodeHandler);
            using var imgStream = new MemoryStream();
            bitmap.Save(imgStream, System.Drawing.Imaging.ImageFormat.Bmp);

            File.WriteAllBytes("c:\\a.jpg", imgStream.ToArray());

            loginButtonHandler.Click();
        }
    }
}
